package com.zj.osm.utils.view_utils

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.zj.osm.App
import com.zj.osm.R
import com.zj.osm.entity.greendao.Equipment
import com.zj.osm.model.pojo.Module
import com.zj.osm.model.pojo.ModuleType
import com.zj.osm.utils.Configuration
import com.zj.osm.utils.SQLiteDatabaseUtils
import com.zj.osm.utils.db_utils.PointsUtils
import com.zj.utils.data.LiveDataBus
import com.zj.utils.view.dialog.CompassDialog
import com.zj.utils.view.dialog.CompassInterface
import kotlinx.android.synthetic.main.activity_equ_fsss_dialog.*
import java.util.*

//箱变
class BoxChangeWindow(context: Context) : AlertDialog(context){
    private var pointId: Long = 0L
    private var projectId: Long = 0L
    private var typeQubie: Int = 7//8水泥制品,9附属设施
    private var equpment: Equipment? = null
    fun setContentViewId(layoutResID: Int): BoxChangeWindow {
        val view = View.inflate(context, layoutResID, null)
        super.setView(view)
        return this
    }

    fun setmapParameters(type: Int, pointId: Long, projectId: Long, equipment: Equipment?): BoxChangeWindow {
        this.typeQubie = type
        this.pointId = pointId
        this.equpment = equipment
        this.projectId = projectId
        initData()
        return this
    }

    lateinit var moduleTypeList: ArrayList<ModuleType>
    fun pop() {
        show()
        bindDataToView()
    }
    private fun bindDataToView() {
        //region 初始化 改造后以及原有，新建，拆除，换装
        initViewAfter()
        //endregion
        //region 初始化改造前的
        if (equpment != null && equpment?.statu == "改造") {
            initBoxChangeWindowBefo()
            alert_boxchang_lt.visibility = View.VISIBLE
        }
        alert_fzx_lt.visibility=View.GONE
        //endregion
       initLisenner()
    }
    private fun initViewAfter() {
        //region 改造后以及原有，新建，拆除，换装
        degreeIv.setOnClickListener {
            val mDialog: Dialog = CompassDialog(this@BoxChangeWindow.context, object : CompassInterface {
                override fun Result(result: String) {
                    progress_Et.setText(result)
                }
            })
            mDialog.show()
        }
        val adapterOne = ArrayAdapter<ModuleType>(context, R.layout.simple_spinner_dropdown_item, moduleTypeList)
        adapterOne.setDropDownViewResource(R.layout.simple_spinner_dropdown_item)
        snzp_ed_lever_one.setAdapter(adapterOne)
        if (equpment != null) {
            progress_Et.setText(equpment!!.angel.toString())
            for (index in moduleTypeList.indices) {
                if (moduleTypeList[index].moduleTypeKey == equpment!!.lever_id) {
                    snzp_ed_lever_one.setSelection(index)
                    break
                }
            }
        } else {
            snzp_ed_lever_one.setSelection(0)
        }
        snzp_ed_lever_one.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val moduleAll = PointsUtils.instance.getAllMoudle(moduleTypeList[position].moduleTypeKey)
                initSnzpTwo(moduleAll)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

        }
        if (equpment != null) {
            val arryState=context.resources.getStringArray(R.array.state)
            for (index in arryState.indices) {
                if (arryState[index] == equpment!!.statu) {
                    alert_showce_state_sp.setSelection(index)
                    break
                }
            }
        } else {
            snzp_ed_lever_one.setSelection(0)
        }
        //状态确定
        alert_showce_state_sp.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
               val statu=alert_showce_state_sp.selectedItem as String
                if (statu == "改造") {
                    initBoxChangeWindowBefo()
                    alert_boxchang_lt.visibility = View.VISIBLE
                }else{
                    alert_boxchang_lt.visibility = View.GONE
                }
            }

        }
        //endregion
    }
    private fun initLisenner() {
        //region 取消按钮
        cancelBt.setOnClickListener {
            dismiss()
        }
        //endregion
        //region 确定按钮
        okBt.setOnClickListener {
            val statu = alert_showce_state_sp.selectedItem as String
            val moduleType = snzp_ed_lever_one.selectedItem as ModuleType
            if (equpment == null) {
                val equipment = Equipment()
                equipment.id = null
                equipment.equipmentId=getUUID()
                equipment.pointId = pointId
                equipment.head_id = "0"
                equipment.lever_id = moduleType.moduleTypeKey
                equipment.name = moduleType.name
                equipment.statu = statu
                equipment.qubie = typeQubie
                equipment.equpmentIcon=moduleType.ico
                if(map_ed_head_two.selectedItem !=null){
                    val module = map_ed_head_two.selectedItem as Module
                    equipment.moduleID = module.moduleID
                    equipment.threeId = module.moduleID
                    equipment.threeName = module.moduleName
                    equipment.module_code = module.moduleCode
                }
                if (progress_Et.text != null && progress_Et.text.toString().isNotEmpty()) {
                    equipment.angel = progress_Et.text.toString().toInt()
                } else {
                    equipment.angel = 0//3
                }
                if (equipment.statu=="改造"){
                    val moduletypeBefo = snzp_ed_lever_one_befo.selectedItem as ModuleType
                    equipment.head_id_before= "0"
                    equipment.lever_id_before= moduletypeBefo.moduleTypeKey
                    equipment.name_before = moduletypeBefo.name
                    if(map_ed_head_two_befo.selectedItem!=null){
                        val module = map_ed_head_two_befo.selectedItem as Module
                        equipment.moduleID_before = module.moduleID
                        equipment.threeName_before = module.moduleName
                        equipment.module_code_before = module.moduleCode
                    }
                    if (progress_Et_befo.text != null && progress_Et_befo.text.toString().isNotEmpty()) {
                        equipment.angle_after = progress_Et_befo.text.toString().toInt()
                    } else {
                        equipment.angle_after = 0//3
                    }

                }
                App.getInstances().daoSession.equipmentDao.insert(equipment)

            } else {
                equpment!!.head_id = "0"
                equpment!!.lever_id = moduleType.moduleTypeKey
                equpment!!.name = moduleType.name
                equpment!!.statu = statu
                if (progress_Et.text != null && progress_Et.text.toString().isNotEmpty()) {
                    equpment!!.angel = progress_Et.text.toString().toInt()
                } else {
                    equpment!!.angel = 0//3
                }
                equpment!!.equpmentIcon=moduleType.ico
                if(map_ed_head_two.selectedItem !=null){
                    val module = map_ed_head_two.selectedItem as Module
                    equpment!!.moduleID = module.moduleID
                    equpment!!.threeName = module.moduleName
                    equpment!!.module_code = module.moduleCode
                }
                if (equpment!!.statu=="改造"){
                    val moduletypeBefo = snzp_ed_lever_one_befo.selectedItem as ModuleType
                    equpment!!.head_id_before= "0"
                    equpment!!.lever_id_before= moduletypeBefo.moduleTypeKey
                    equpment!!.name_before = moduletypeBefo.name
                    if(map_ed_head_two_befo.selectedItem!=null){
                        val module = map_ed_head_two_befo.selectedItem as Module
                        equpment!!.moduleID_before = module.moduleID
                        equpment!!.threeName_before = module.moduleName
                        equpment!!.module_code_before = module.moduleCode
                    }
                    if (progress_Et_befo.text != null && progress_Et_befo.text.toString().isNotEmpty()) {
                        equpment!!.angle_after = progress_Et_befo.text.toString().toInt()
                    } else {
                        equpment!!.angle_after = 0//3
                    }

                }
                App.getInstances().daoSession.equipmentDao.update(equpment)
            }
            LiveDataBus.get().getChannel(Configuration.BoxChangeWindow).value = 1
            dismiss()
        }
        //endregion
    }
    //改造前的
    private fun initBoxChangeWindowBefo() {
        degreeIv_befo.setOnClickListener {
            val mDialog: Dialog = CompassDialog(this@BoxChangeWindow.context, object : CompassInterface {
                override fun Result(result: String) {
                    progress_Et_befo.setText(result)
                }
            })
            mDialog.show()
        }
        val adapterOne = ArrayAdapter<ModuleType>(context, R.layout.simple_spinner_dropdown_item, moduleTypeList)
        adapterOne.setDropDownViewResource(R.layout.simple_spinner_dropdown_item)
        snzp_ed_lever_one_befo.adapter = adapterOne
        if (equpment != null) {
            progress_Et_befo.setText(equpment!!.angle_after.toString())
            for (index in moduleTypeList.indices) {
                if (moduleTypeList[index].moduleTypeKey == equpment!!.lever_id_before) {
                    snzp_ed_lever_one_befo.setSelection(index)
                    break
                }
            }
        } else {
            snzp_ed_lever_one_befo.setSelection(0)
        }
        snzp_ed_lever_one_befo.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val moduleAll = PointsUtils.instance.getAllMoudle(moduleTypeList[position].moduleTypeKey)
                initSnzpTwoBefo(moduleAll)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

        }


    }

    private fun initSnzpTwoBefo(moduleAll: ArrayList<Module>) {
        val adapterTwo = ArrayAdapter<Module>(context, R.layout.simple_spinner_dropdown_item, moduleAll)
        adapterTwo.setDropDownViewResource(R.layout.simple_spinner_dropdown_item)
        map_ed_head_two_befo.adapter = adapterTwo
        if(equpment!=null) {
            for (index in moduleAll.indices) {
                if (moduleAll[index].moduleID == equpment!!.moduleID_before) {
                    map_ed_head_two_befo.setSelection(index)
                    break
                }
            }
        }else{
            map_ed_head_two_befo.setSelection(0)
        }
        map_ed_head_two_befo.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }


        }
    }

    //柱上变压器 方案
    private fun initSnzpTwo(moduleAll: ArrayList<Module>) {

        val adapterTwo = ArrayAdapter<Module>(context, R.layout.simple_spinner_dropdown_item, moduleAll)
        adapterTwo.setDropDownViewResource(R.layout.simple_spinner_dropdown_item)
        map_ed_head_two.adapter = adapterTwo
        if(equpment!=null) {
            for (index in moduleAll.indices) {
                if (moduleAll[index].moduleID == equpment!!.moduleID) {
                    map_ed_head_two.setSelection(index)
                    break
                }
            }
        }else{
            map_ed_head_two.setSelection(0)
        }
        map_ed_head_two.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }


        }
    }

    /*
    * 获取数据
    * */
    private fun initData() {
        val moduTypkeyArray = arrayOf(28)
        moduleTypeList = PointsUtils.instance.getAllMoudleTypeOfBoxChange(moduTypkeyArray)
    }
    /**
     * 获取UUID
     *
     * @return uuid
     */
    private fun getUUID(): String? {
        return UUID.randomUUID().toString() + ""
    }
}
